﻿using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using Zdm.Crm.Client.Entity.Entity;

namespace PropertyManagement.Client.WinFormClient.Forms.Utilies
{
    public class GridUtil
    {
        /// <summary>
        /// 获取表格中的一行数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="view"></param>
        /// <returns></returns>
        public static T GetSelectedSingleRow<T>(GridView view) where T : BaseEntity, new()
        {
            int[] selRows = view.GetSelectedRows();
            if (selRows == null || selRows.Length == 0)
            {
                XtraMessageBox.Show("请选中一条数据后，再进行修改。");
                return new T { IsDeleted = true };
            }
            if (selRows.Length > 1)
            {
                XtraMessageBox.Show("不能选择两条记录。");
                return new T { IsDeleted = true };
            }
            return view.GetRow(selRows[0]) as T;
        }


        public static List<T> GetSelectedRows<T>(GridView view) where T : BaseEntity, new()
        {
            int[] selRows = view.GetSelectedRows();
            if (selRows == null || selRows.Length == 0)
            {
                XtraMessageBox.Show("请选中一条以上的数据后，再进行修改。");
                return new List<T>();
            }
            return selRows.Select(selRow => view.GetRow(selRow) as T).ToList();
        }

        /// <summary>
        /// 导出EXCEL文件
        /// </summary>
        /// <param name="gc"></param>
        /// <param name="defaultName"></param>
        public static void ToXls(GridControl gc, string defaultName)
        {
            string fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls", defaultName);
            if (string.IsNullOrEmpty(fileName))
                return;
            gc.ExportToXls(fileName);

            OpenFile(fileName);
        }


        /// <summary>
        /// 文件选择提示窗体
        /// </summary>
        /// <param name="title"></param>
        /// <param name="filter"></param>
        /// <param name="defaultName"></param>
        /// <returns></returns>
        private static string ShowSaveFileDialog(string title, string filter, string defaultName)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            string name = defaultName;
            int n = name.LastIndexOf(".") + 1;
            if (n > 0) name = name.Substring(n, name.Length - n);
            dlg.Title = "导出到 " + title;
            dlg.FileName = name;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
            return "";
        }
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="fileName"></param>
        private static void OpenFile(string fileName)
        {
            if (XtraMessageBox.Show("您想打开文件吗?", "导出...", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    System.Diagnostics.Process process = new System.Diagnostics.Process();
                    process.StartInfo.FileName = fileName;
                    process.StartInfo.Verb = "Open";
                    process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
                    process.Start();
                }
                catch
                {
                    XtraMessageBox.Show("在您的机器上找不到能打开此类文件的程序。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

    }
}
